<template>
  <div>
    <!-- <div class="n-layout-page-header">
      <n-card :bordered="false">
         这是由系统生成的CURD表格，你可以将此行注释改为表格的描述
      </n-card>
    </div> -->
    <n-card :bordered="false" class="proCard" title="商品中心">
      <BasicForm  ref="searchFormRef" @register="register" @submit="reloadTable" @reset="reloadTable" @keyup.enter="reloadTable">
        <template #statusSlot="{ model, field }">
          <n-input v-model:value="model[field]" />
        </template>
      </BasicForm>
      <BasicTable ref="actionRef" :columns="columns" :request="loadDataTable" :row-key="(row) => row.id" :actionColumn="actionColumn" :scroll-x="scrollX" :resizeHeightOffset="-10000"  @update:sorter="handleUpdateSorter">
        <template #tableTitle>
          <n-button type="primary"  @click="addTable" class="min-left-space" v-if="hasPermission(['/product/edit'])">
            <template #icon>
              <n-icon>
                <PlusOutlined />
              </n-icon>
            </template>
            添加
          </n-button>
          <n-button type="primary" @click="cateManger" class="min-left-space">
            <template #icon>
              <n-icon>
                <PlusOutlined />
              </n-icon>
            </template>
            分类管理
          </n-button>
        </template>
      </BasicTable>
    </n-card>
    <Edit ref="editRef" @reloadTable="reloadTable" />
    <Category ref="cateRef"></Category>
  </div>
</template>

<script lang="ts" setup>
import { List, Status } from '@/api/product';
import { BasicForm, useForm } from '@/components/Form/index';
import { BasicTable, TableAction } from '@/components/Table';
import { useSorter } from '@/hooks/common';
import { usePermission } from '@/hooks/web/usePermission';
import { useDictStore } from '@/store/modules/dict';
import { adaTableScrollX } from '@/utils/hotgo';
import { PlusOutlined } from '@vicons/antd';
import { useDialog, useMessage } from 'naive-ui';
import { computed, h, onMounted, reactive, ref } from 'vue';
import Edit from './edit.vue';
import Category from './category.vue';
import { columns, loadOptions, schemas, State } from './model';

  const dict = useDictStore();
  const dialog = useDialog();
  const message = useMessage();
  const { hasPermission } = usePermission();
  const actionRef = ref();
  const searchFormRef = ref<any>({});
  const editRef = ref();
  const cateRef = ref();
  const { updateSorter: handleUpdateSorter, sortStatesRef: sortStatesRef } = useSorter(reloadTable);

  sortStatesRef.value = [
    {
      columnKey: 'createdAt',
      order: 'descend',
      sorter: 'default',
    },
  ];

  const actionColumn = reactive({
    width: 144,
    title: '操作',
    key: 'action',
    fixed: 'right',
    render(record: State) {
      return h(TableAction as any, {
        style: 'button',
        actions: [
          {
            label: '编辑',
            onClick: handleEdit.bind(null, record),
            auth: ['/product/edit'],
          },

          {
            label: '下架',
            onClick: handleStatus.bind(null, record, 2),
            ifShow: () => {
              return record.status === 1;
            },
            auth: ['/product/status'],
            type: 'default',
          },
          {
            label: '上架',
            onClick: handleStatus.bind(null, record, 1),
            ifShow: () => {
              return record.status === 2;
            },
            auth: ['/product/status'],
            type: 'warning',
          },
        ],
      });
    },
  });

  const scrollX = computed(() => {
    return adaTableScrollX(columns, actionColumn.width);
  });

  const [register, {}] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    labelWidth: 80,
    schemas,
  });

  // 加载表格数据
  const loadDataTable = async (res) => {
    return await List({ ...searchFormRef.value?.formModel, ...res, ...{ sorters: sortStatesRef.value } });
  };

  // 重新加载表格数据
  function reloadTable() {
    actionRef.value?.reload();
  }

  // 添加数据
  function addTable() {
    editRef.value.openModal(null);
  }

  const cateManger = () => {
    cateRef.value.openModal(null);
  };

  // 编辑数据
  function handleEdit(record: Recordable) {
    editRef.value.openModal(record);
  }

  // 修改状态
  function handleStatus(record: Recordable, status: number) {
    Status({ id: record.id, status: status }).then((_res) => {
      message.success('设置成功');
      setTimeout(() => {
        reloadTable();
      });
    });
  }

  onMounted(() => {
    loadOptions();

  });
</script>

<style lang="less" scoped></style>